import * as echarts from 'echarts/core';
import {
    TitleComponent,
    TooltipComponent,
    GridComponent,
    LegendComponent,
    ToolboxComponent
} from 'echarts/components';
import { LineChart } from 'echarts/charts';
import { UniversalTransition } from 'echarts/features';
import { CanvasRenderer } from 'echarts/renderers';
import { useEffect } from 'react';

echarts.use([
    TitleComponent,
    TooltipComponent,
    GridComponent,
    LegendComponent,
    LineChart,
    CanvasRenderer,
    UniversalTransition,
    ToolboxComponent
]);

const option = {
    title: {
        text: '访客记录图表'
    },
    tooltip: {
        trigger: 'axis',
        axisPointer: { // hover有十字标记
            type: 'cross'
        }
    },
    legend: {
        data: ['近七天总数', '外来学生', '外来老师', 'Direct', 'Search Engine']
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    xAxis: {
        show: true,
        type: 'category',
        // boundaryGap: false,
        axisLine: {
            show: true, // 是否显示x轴线
            lineStyle: {
                color: '#555' // 轴线颜色
            }
        },
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    yAxis: [
        {
            show: true,
            // boundaryGap: true,
            type: 'value',
            name: '人数',
            position: 'left',
            alignTicks: true,
            axisLabel: {
                formatter: '{value} ' // value单位
            },
            axisLine: {
                show: true, // 是否显示y轴线
                lineStyle: {
                    color: '#555'
                }
            }
        }
    ],
    series: [
        {
            name: '近七天总数',
            type: 'line',
            stack: 'Total',
            data: [120, 132, 101, 134, 90, 230, 210]
        },
        {
            name: '外来学生',
            type: 'line',
            stack: 'Total',
            data: [220, 182, 191, 234, 290, 330, 310]
        },
        {
            name: '外来老师',
            type: 'line',
            stack: 'Total',
            data: [150, 232, 201, 154, 190, 330, 410]
        },
        {
            name: 'Direct',
            type: 'line',
            stack: 'Total',
            data: [320, 332, 301, 334, 390, 330, 320]
        },
        {
            name: 'Search Engine',
            type: 'line',
            stack: 'Total',
            data: [820, 932, 901, 934, 1290, 1330, 1320]
        }
    ]
};

const EchartLine = (props) => {
    const { echartId, style } = props
    console.log('初始化', props)
    const defaultStyle = { width: '100%', height: '500px' }

    useEffect(() => {
        console.log('挂载')
        let chartDom = document.getElementById(echartId);
        let myChart = echarts.init(chartDom);
        setTimeout(() => {
            myChart.resize()
        }, 10);
        // 监听窗口变化
        window.addEventListener('resize', ()=> {
            myChart.resize()
        })
        myChart.setOption(option);
    
        // 组件即将销毁
        return () => {
            // 销毁echart实例
            console.log('组件即将被销毁')
            myChart.dispose()
        }
        
    })

    console.log('渲染...')
    return (
        <div id={echartId} className='myechart' style={(JSON.stringify(style) === '{}' || !style) ? defaultStyle : style}></div>
    );
}

export default EchartLine;